package com.erclab.internal.xpresso.xml;

import java.util.ArrayList;

import com.erclab.internal.xpresso.reportelements.ReportElement;
import com.erclab.internal.xpresso.reports.Report;
import com.erclab.internal.xpresso.reports.ReportFilter;
import com.erclab.internal.xpresso.reports.ReportResultSet;

public class XmlReportHelper {

	public static void writeReportTags(Report report, StringBuffer xml) {
		xml.append("<title>");
		xml.append(report.getTitle());
		xml.append("</title>");

		xml.append("<uniqueKeyName>");
		xml.append(report.getUniqueKeyName());
		xml.append("</uniqueKeyName>");
	}
	
	public static void writeFilterTags(Report report, StringBuffer xml) {
		ArrayList filters = report.getReportFilters();
		for (int i =0; i < filters.size(); i++){
			ReportFilter f = (ReportFilter)filters.get(i);
			xml.append("<filterElement>");
			
			xml.append("<flexClass>");
			xml.append(f.getFlexClass());
			xml.append("</flexClass>");

			xml.append("<columnToFilter>");
			xml.append(f.getColumnToFilter());
			xml.append("</columnToFilter>");

			xml.append("<parameters>");
			xml.append(f.getParameters());
			xml.append("</parameters>");

			xml.append("<label>");
			xml.append(f.getLabel());
			xml.append("</label>");

			xml.append("<style>");
			xml.append(f.getStyle());
			xml.append("</style>");
			
			xml.append("</filterElement>");
		}
	}
	
	public static void writeColumnTags(Report report, StringBuffer xml) {
		ArrayList columns = report.getReportElements();
		for (int i =0; i < columns.size(); i++){
			xml.append("<column>");
			ReportElement re = (ReportElement)columns.get(i);
			xml.append("<HeaderText>");
			xml.append(re.getHeaderText());
			xml.append("</HeaderText>");

			xml.append("<ColumnName>");
			xml.append(re.getColumnName());
			xml.append("</ColumnName>");

			xml.append("<Style>");
			xml.append(re.getStyle());
			xml.append("</Style>");

			xml.append("<FlexClass>");
			xml.append(re.getFlexClass());
			xml.append("</FlexClass>");			

			xml.append("<Parameters>");
			xml.append(re.getParameters());
			xml.append("</Parameters>");				
			
			xml.append("</column>");
		}
	}	
	
	public static void writeReportResultSet(Report report, StringBuffer xml) {
		ReportResultSet rrs = report.getReportResultSet();
		while(rrs.nextRow()){
			xml.append("<record>");
			for (int i =0; i < rrs.getNumberOfColumns(); i++){
				xml.append("<" + rrs.getColumnName(i) + ">");
				xml.append("<![CDATA[");
				xml.append(rrs.getRowValue(i));
				xml.append("]]>");
				xml.append("</" + rrs.getColumnName(i) + ">");
			}
			xml.append("</record>");
		}
	}	
	
}
